<?php
/**
 * Created by PhpStorm.
 * User: 郝飞
 * Date: 2017/7/7
 * Time: 9:30
 */

namespace Publish\Controller;
use Common\Controller\AdminbaseController;

class AdminStoryController extends AdminbaseController{
	protected $story_model;

	function _initialize() {
		parent::_initialize();
		$this->story_model = D("Publish/PubStory");
	}

	/*人物匹配*/
	public function Character(){
		$keyword = I('queryString');
		$user_info = \Publish\Lib\ImiLib::Choose($keyword);
		$html = '';
		foreach($user_info as $key => $val){
			$html .= '<li onClick="fill(\''.$val['id'].'\',\''.$val['name'].'\');">'.$val['name'].'</li>';
		}
		echo $html;
	}

	public function index(){
		/*搜索条件*/
		$start_time=I('request.start_time');
		if(!empty($start_time)){
			$where['a.createtime']=array(
				array('EGT',strtotime($start_time))
			);
		}
		$end_time=I('request.end_time');
		if(!empty($end_time)){
			if(empty($where['a.createtime'])){
				$where['a.createtime']=array();
			}
			array_push($where['a.createtime'], array('ELT',strtotime($end_time)));
		}
		$keyword=I('request.keyword');
		if(!empty($keyword)){
			$where['a.title|b.name']=array('like',"%$keyword%");
		}

		/*分页--带条件查询分页*/
		$count = $this->story_model
			->alias("a")
			->join('__PUB_PERSON__ as b ON a.person_id = b.id')
			->field('a.*,b.name')
			->order('a.id asc,a.story desc,a.createtime desc')
			->where($where)
			->count();
		$page = $this->page($count, 20);
		$storys = $this->story_model
			->alias("a")
			->join('__PUB_PERSON__ as b ON a.person_id = b.id')
			->field('a.*,b.name')
			->order('a.id asc,a.story desc,a.createtime desc')
			->where($where)
			->limit($page->firstRow , $page->listRows)
			->select();
			foreach ($storys as $k => $v) {
				$new[$v['person_id']][]=$v;
			}
		$this->assign("formget",array_merge($_GET,$_POST));
		$this->assign("page", $page->show('Admin'));
		$this->assign("story",$new);
		$this->display();
	}

	public function add(){
		$this->display();
	}

	public function add_post(){
		if(IS_POST){
			empty($_POST['person_id']) && $this->error("请选择人物");
			if(!empty($_POST['music'])){
				$musicurl = sp_asset_relative_url($_POST['music']);
			}
			$_POST['content']=htmlspecialchars_decode($_POST['content']);
			$data['person_id'] = I("person_id");
			$data['title'] = I("title");
			$data['music'] = $musicurl?$musicurl:null;
			$data['mname']=I('mname');
			$data['content'] = I("content");
			$data['createtime'] = time();
			if($this->story_model->create($data)){
				if($this->story_model->add($data)){
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}
		}
	}

	public function edit(){
		$id=  I("get.id",0,'intval');
		$storys = $this->story_model
			->alias("a")
			->join('__PUB_PERSON__ as b ON a.person_id = b.id')
			->field('a.*,b.name')
			->where(array('a.id'=>$id))
			->find();
		$this->assign("story",$storys);
		// dump($storys);
		$this->display();
	}

	public function edit_posts(){
		if(IS_POST){
			$story_id=I("id",0,'intval');

			//上传音频时，将就数据删除
			$story_infomation = \Publish\Lib\StoryLib::IsStory($story_id);
			$story_music_url = $story_infomation['music'];
			empty($_POST['person_id']) && $this->error("请选择人物");
			if(!empty($_POST['music'])){
				//判断是否修改音频文件 $is_status 删除状态
				if($_POST['music'] != $story_music_url) if(!empty($story_music_url)) $is_status = \Publish\Lib\ImiLib::UnlickFile($story_music_url);
				$musicurl = sp_asset_relative_url($_POST['music']);
			}
			$_POST['content']=htmlspecialchars_decode($_POST['content']);
			$data['person_id'] = I("person_id");
			$data['title'] = I("title");
			$data['music'] = $musicurl?$musicurl:null;
			$data['content'] = I("content");
			$data['createtime'] = time();
			if($this->story_model->create($data)){
				if($this->story_model->where(array('id'=>$story_id))->save($data)){
					$this->success("修改成功！",U('Publish/AdminStory/index'));
				}else{
					$this->error("修改失败！");
				}
			}
		}
	}
	public function edit_post(){
		// $id= I("get.id",0,'intval');
		$id=$_POST['id']; /*135*/
		$old=$this->story_model->where("id=$id")->find();
		if (IS_POST) {
			// empty($_POST['person_id']) && $this->error("请选择人物");
			$old_id=$_POST['person_id']; /*1*/
			$data['person_id']=$_POST['person_id'];
			$data['content']=$_POST['content'];
			$data['mname']=$_POST['mname'];
			$data['music']=$_POST['music'];
			$data['title']=$_POST['title'];
			$data['time'] = time();
			// $data['id']=$id;
			// if($this->history_model->where("id=$id")->create($data)){
				$this->story_model->where("id=$id")->save($data);
					unlink(C('UPLOADPATH').$old['music']);
					$this->success("修改成功！",U('Publish/AdminStory/index'));
				}else{
					$this->error("修改失败！");
				}
	}
	public function delete(){
		if(isset($_GET['id'])){
			$id = I("get.id",0,'intval');
			if ($this->story_model->where(array('id'=>$id))->delete()) {
				//删除时，删除对应音频文件
				$story_infomation = \Publish\Lib\StoryLib::IsStory($id);
				$story_music_url = $story_infomation['music'];
				//$is_status 删除状态
				if(!empty($story_music_url)) $is_status = \Publish\Lib\ImiLib::UnlickFile($story_music_url);
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}

		if(isset($_POST['ids'])){
			$ids = I('post.ids/a');
			/*删除多个数据时，循环删除对应文件*/
			for($i=0;$i<count($ids);$i++){
				//删除时，删除对应音频文件
				$story_infomation = \Publish\Lib\StoryLib::IsStory($ids[$i]);
				$story_music_url = $story_infomation['music'];
				//$is_status 删除状态
				if(!empty($story_music_url)) $is_status = \Publish\Lib\ImiLib::UnlickFile($story_music_url);
			}
			if ($this->story_model->where(array('id'=>array('in',$ids)))->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}


// 最后
}